package com.hrg.seer.sdk.rbk;


import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class RbkClient {
    @Getter
    private AtomicInteger connectOkCnt = new AtomicInteger(0);
    private final String host;
    // 机器人配置API（端口：19207）
    RbkPortClient configClient;
    // 其他API（端口：19210）
    RbkPortClient miscClient;
    // 机器人状态API（端口：19204）
    RbkPortClient statusClient;
    // 机器人控制API（端口：19205）
    RbkPortClient controlClient;
    // 机器人导航API（端口：19206）
    RbkPortClient navClient;
    // 机器人推送API（端口：19301）
    RbkPortClient pushClient;

    public RbkClient(String host) {
        this.host = host;
    }

    public void initClient() {
        this.statusClient = new RbkPortClient(host, 19204);
        this.controlClient = new RbkPortClient(host, 19205);
        this.navClient = new RbkPortClient(host, 19206);
        this.configClient = new RbkPortClient(host, 19207);
        this.miscClient = new RbkPortClient(host, 19210);
        this.pushClient = new RbkPortClient(host, 19301);
    }
    
    public boolean isIfConnectOk() {return this.connectOkCnt.get() == 6; }

    //释放资源
    /*
     * Releasing the connection to the robot
     */
    public void dispose() {
        statusClient.dispose();
        controlClient.dispose();
        navClient.dispose();
        configClient.dispose();
        miscClient.dispose();
        pushClient.dispose();
    }

    /*
     * @param apiNo  rbk api no
     * @param requestStr  rbk request json
     * @param timeout default 10s
     * @return RbkResult {"kind": Ok|NoSuchRobot|ConnectFail|WriteError|Disposed|BadApiNo|Timeout|Interrupted  Only ok means the request for rbk was successful
     *                     "ip": robot ip    (string)
     *                     "apiNo": rbk request num (int)
     *                     "reqStr": rbk request body (String)
     *                     "resStr": rbk response body (String)
     *                      "errMsg": request rbk error info
     *                      }
     */
    public RbkResult request(int apiNo, String requestStr) {
        return this.request(apiNo, requestStr, 0);
    }
    public RbkResult request(int apiNo, String requestStr, long timeout) {
        log.debug("apiNo {} requestStr {} timeout {}", apiNo, requestStr, timeout);
        if (!isIfConnectOk()) {
            return RbkResult.builder().kind(RbkResultKind.ConnectFail).ip(host).apiNo(apiNo).resStr(requestStr).build();
        }
        if (timeout == 0) {
            timeout = 10 * 1000;
        }
        requestStr = requestStr == null ? "" : requestStr;
        if (apiNo >= 1000 && apiNo <= 1999) {//rbk tcp协议 编号
            return statusClient.request(apiNo, requestStr, timeout);
        } else if (apiNo >= 2000 && apiNo <= 2999) {
            return controlClient.request(apiNo, requestStr, timeout);
        } else if (apiNo >= 3000 && apiNo <= 3999) {
            return navClient.request(apiNo, requestStr, timeout);
        } else if (apiNo >= 4000 && apiNo <= 5999) {
            return configClient.request(apiNo, requestStr, timeout);
        } else if (apiNo >= 6000 && apiNo <= 6999) {
            return miscClient.request(apiNo, requestStr, timeout);
        } else if (apiNo == 9300) {
            return pushClient.request(apiNo, requestStr, timeout);
        } else {
            return RbkResult.builder().kind(RbkResultKind.BadApiNo).ip(host).apiNo(apiNo).resStr(requestStr).build();
        }
    }


}
